package database

import (
	"database/sql"
	"testing"
	"time"
)

// mockDB 模拟数据库查询结果
type mockDB struct {
	rows *sql.Rows
	err  error
}

// 保存原始函数的变量
var originalGetVideoSessionByRoomID func(roomID string) (*VideoSession, error)

// 测试IsValidSession函数
func TestIsValidSession(t *testing.T) {
	// 保存原始函数
	originalGetVideoSessionByRoomID = GetVideoSessionByRoomID
	
	// 测试结束后恢复原始函数
	defer func() {
		GetVideoSessionByRoomID = originalGetVideoSessionByRoomID
	}()
	
	// 测试用例
	tests := []struct {
		name       string
		roomID     string
		userID     uint64
		isCustomer bool
		mockFn     func(roomID string) (*VideoSession, error)
		wantValid  bool
		wantErr    bool
	}{
		{
			name:       "有效的客户会话",
			roomID:     "room123",
			userID:     1001,
			isCustomer: true,
			mockFn: func(roomID string) (*VideoSession, error) {
				// 模拟一个有效的会话
				now := time.Now()
				return &VideoSession{
					SessionID:   "session123",
					OrderID:     "order123",
					RoomID:      "room123",
					CustomerID:  1001,
					ProviderID:  2001,
					SessionType: TypeLive,
					Status:      StatusConnected,
					StartTime:   &now,
					CreatedAt:   now,
					UpdatedAt:   now,
				}, nil
			},
			wantValid: true,
			wantErr:   false,
		},
		{
			name:       "有效的服务提供者会话",
			roomID:     "room123",
			userID:     2001,
			isCustomer: false,
			mockFn: func(roomID string) (*VideoSession, error) {
				// 模拟一个有效的会话
				now := time.Now()
				return &VideoSession{
					SessionID:   "session123",
					OrderID:     "order123",
					RoomID:      "room123",
					CustomerID:  1001,
					ProviderID:  2001,
					SessionType: TypeLive,
					Status:      StatusConnected,
					StartTime:   &now,
					CreatedAt:   now,
					UpdatedAt:   now,
				}, nil
			},
			wantValid: true,
			wantErr:   false,
		},
		{
			name:       "会话已结束",
			roomID:     "room123",
			userID:     1001,
			isCustomer: true,
			mockFn: func(roomID string) (*VideoSession, error) {
				// 模拟一个已结束的会话
				now := time.Now()
				end := now.Add(time.Hour)
				return &VideoSession{
					SessionID:   "session123",
					OrderID:     "order123",
					RoomID:      "room123",
					CustomerID:  1001,
					ProviderID:  2001,
					SessionType: TypeLive,
					Status:      StatusEnded,
					StartTime:   &now,
					EndTime:     &end,
					CreatedAt:   now,
					UpdatedAt:   now,
				}, nil
			},
			wantValid: false,
			wantErr:   false,
		},
		{
			name:       "用户ID不匹配",
			roomID:     "room123",
			userID:     9999,
			isCustomer: true,
			mockFn: func(roomID string) (*VideoSession, error) {
				// 模拟一个有效的会话，但用户ID不匹配
				now := time.Now()
				return &VideoSession{
					SessionID:   "session123",
					OrderID:     "order123",
					RoomID:      "room123",
					CustomerID:  1001, // 不匹配测试用例中的9999
					ProviderID:  2001,
					SessionType: TypeLive,
					Status:      StatusConnected,
					StartTime:   &now,
					CreatedAt:   now,
					UpdatedAt:   now,
				}, nil
			},
			wantValid: false,
			wantErr:   false,
		},
		{
			name:       "会话不存在",
			roomID:     "nonexistent",
			userID:     1001,
			isCustomer: true,
			mockFn: func(roomID string) (*VideoSession, error) {
				// 模拟会话不存在
				return nil, nil
			},
			wantValid: false,
			wantErr:   false,
		},
		{
			name:       "数据库错误",
			roomID:     "room123",
			userID:     1001,
			isCustomer: true,
			mockFn: func(roomID string) (*VideoSession, error) {
				// 模拟数据库错误
				return nil, sql.ErrConnDone
			},
			wantValid: false,
			wantErr:   true,
		},
	}
	
	// 执行测试用例
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// 设置模拟函数
			GetVideoSessionByRoomID = tt.mockFn
			
			// 执行测试
			gotValid, err := IsValidSession(tt.roomID, tt.userID, tt.isCustomer)
			
			// 检查错误
			if (err != nil) != tt.wantErr {
				t.Errorf("IsValidSession() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			
			// 检查结果
			if gotValid != tt.wantValid {
				t.Errorf("IsValidSession() = %v, want %v", gotValid, tt.wantValid)
			}
		})
	}
}